package kk.learn.rabbitmq.dlxdemo.consumer;


import com.rabbitmq.client.Channel;
import kk.learn.rabbitmq.dlxdemo.service.IOrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <p>
 *
 * </p>
 *
 * @author KK
 * @since 2021-10-30
 */
@Component
public class CheckOrderStatusListener {


    private final IOrderService orderService;

    public CheckOrderStatusListener(IOrderService orderService) {
        this.orderService = orderService;
    }


    @RabbitListener(queues = "q.order.dlx")
    public void checkOrderStatus(Message message, Channel channel, @Payload Long orderId) throws IOException {


        System.out.println("需要检查的orderId：  " + orderId);
        //检查订单状态, 如果还是待支付, 更改状态未支付超时.
        orderService.checkOrderStatus(orderId);



        // 确认消息
        final long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, false);


    }


}
